package com.algorithm.ch1.yjx.array;

import java.util.Arrays;

/**
 * @author yuanjianxin
 * @date 2018/8/13 19:58
 */
public class Intersect {

    public static void main(String[] args) {
        int[] arr1 = {3, 2, 1};
        int[] arr2 = {1, 1};

        int[] intersect = intersect(arr1, arr2);
        System.out.println(Arrays.toString(intersect));

    }

    public static int[] intersect(int[] nums1, int[] nums2) {
        int index = 0;
        int temp2 = 0;
        for (int i = 0; i < nums1.length; i++) {
            for (int j = index; j < nums2.length; j++) {
                if (nums1[i] == nums2[j]) {
                    temp2 = nums2[index];
                    nums2[index] = nums1[i];
                    nums2[j] = temp2;
                    index++;
                    break;
                }
            }
        }
        int[] arr = new int[index];
        for (int i = 0; i < index; i++) {
            arr[i] = nums2[i];
        }
        return arr;
    }
}
